#!/bin/sh

# get internal IP address
# used for outgoing Internet connections
# see: https://github.com/rsp/scripts/blob/master/internalip.md

resolve() {
	(gethostip -d $1 || getent ahostsv4 $t | grep RAW | awk '{print $1; exit}') 2>/dev/null
}
noip() {
	[ -n "$(echo $1 | tr -d '0-9.\n')" ]
}

[ -n "$1" ] && t=$1 || t='8.8.8.8'

noip $t && t=$(resolve $t)

[ -n "$t" ] || { echo Cannot resolve domain $1 >&2; exit 1; }

ip route get $t | awk '{print $NF; exit}'
